import * as vscode from "vscode";
import getFilePath from "@ap/util/vscode/getFilePath";
import { showErrorMessage } from "./message";
import { arenaErrorMsg } from "./errorMsg";
// 命令执行函数
export default function openAndWriteTextDocument(option: {
  language: string;
  content: string;
  viewColumn: vscode.ViewColumn;
}) {
  // 创建一个新文件
  vscode.workspace
    .openTextDocument({ language: option.language })
    .then((doc) => {
      // 显示新创建的文档
      vscode.window.showTextDocument(doc, option.viewColumn).then((editor) => {
        // 在文件开头插入文本
        editor.edit((editBuilder) => {
          editBuilder.insert(new vscode.Position(0, 0), option.content);
        });
        vscode.commands.executeCommand("editor.action.formatDocument");
      });
    });
}

export function openFileDocument(path: string) {
  const filePath = getFilePath();
  if (!filePath) {
    showErrorMessage(arenaErrorMsg.OPEN_WORKSPACE);
    return;
  }
  // 创建一个新文件
  vscode.workspace
    .openTextDocument(vscode.Uri.file(filePath + path))
    .then((doc) => {
      // 显示新创建的文档
      vscode.window.showTextDocument(doc);
    });
}

export function openTempFileDocument(
  path: vscode.Uri,
  viewColumn?: vscode.ViewColumn,
) {
  vscode.workspace.openTextDocument(path).then((doc) => {
    // 显示新创建的文档
    vscode.window.showTextDocument(doc, viewColumn).then((editor) => {
      // vscode.commands.executeCommand("editor.action.formatDocument");
    });
  });
}
